package com.example.guiguihomemedical.controller;

import com.example.guiguihomemedical.common.Result;
import com.example.guiguihomemedical.entity.Doctor;
import com.example.guiguihomemedical.entity.Record;
import com.example.guiguihomemedical.entity.dto.RecordList;
import com.example.guiguihomemedical.service.DoctorService;
import com.example.guiguihomemedical.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/record")
public class RecordController {
    @Autowired
    private RecordService recordService;

    @PostMapping("/addrecord")
    public Result add(@RequestBody Record record) {
        recordService.addRecord(record);
        return Result.success("成功！");
    }

    @PutMapping("/update")
    public Result updateRecord(@RequestBody Record record) {
        try {
            recordService.updateRecord(record);
            return Result.success("成功更新记录！");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("更新记录失败：" + e.getMessage());
        }
    }

    @DeleteMapping("/delete/{idcard}")
    public Result deleteRecord(@PathVariable String idcard) {
        try {
            recordService.deleteRecordById(idcard);
            return Result.success("成功删除记录！");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除记录失败：" + e.getMessage());
        }
    }

    @GetMapping("/records/{idcard}")
    public Result getRecordById(@PathVariable String idcard) {
        Record record = recordService.findByIdcard(idcard);
        if (record != null) {
            return Result.success(record);
        } else {
            return Result.error("Record not found");
        }
    }

    @GetMapping("/recordById")
    public Result getRecordById2(@RequestParam(required = false, value = "IdCard") String IdCard) {
        Record record = recordService.findByIdcard(IdCard);
        if (record != null) {
            return Result.success(record);
        } else {
            return Result.error("Record not found");
        }
    }
    //返回全部
    @GetMapping("/all")
    public Result getAllRecords() {
        List<Record> records = recordService.findAllRecords();
        return Result.success(records);
    }
    //模糊查询
    @GetMapping("/list")
    public Result getRecordList(@RequestParam(required = false) String query){
        List<Record> record= recordService.recordList(query);
        return Result.success(record);
    }

    @GetMapping("/idcard/{idcard}")
    public Result getRecordByIdcard(@PathVariable String idcard){
        RecordList recordList =recordService.getRecordByIdcard(idcard);
        return Result.success(recordList);
    }
}
